package com.example.freeengine.filetranslate;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import android.util.Log;
import android.view.MenuItem;
import android.view.WindowManager;

import com.example.freeengine.MainActivity;
import com.example.freeengine.R;
import com.example.freeengine.filetranslate.constants.FileTranslateConstant;
import com.example.freeengine.filetranslate.fragment.ClientInfo;
import com.example.freeengine.filetranslate.fragment.FileInfo;
import com.example.freeengine.filetranslate.fragment.RequestMessage;
import com.example.freeengine.filetranslate.fragment.ResponseMessage;
import com.example.freeengine.filetranslate.persistence.entity.FileTranslateProgress;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.koushikdutta.async.AsyncServer;
import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.http.WebSocket;
import com.koushikdutta.async.http.server.AsyncHttpServer;
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
import com.koushikdutta.async.http.server.HttpServerRequestCallback;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

public class FileTranslateFragment extends AppCompatActivity {

    private static final String TAG = FileTranslateFragment.class.getSimpleName();
    private FileTranslateViewModel mViewModel;

    public static FileTranslateFragment newInstance() {
        return new FileTranslateFragment();
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file_translate_fragment);

        mViewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(FileTranslateViewModel.class);

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.

        BottomNavigationView navigationView = findViewById(R.id.bnv_filetranslate);

        Toolbar toolbar = findViewById(R.id.tb_filetranslate);
        toolbar.setTitle("接收");

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView2);
        NavController navController = navHostFragment.getNavController();
        navController.setGraph(R.navigation.nav_grap_filetranslate);
        NavigationUI.setupWithNavController(navigationView, navController);

        // 状态栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }

        if (mViewModel.getNetStatus() == 1 || mViewModel.getNetStatus() == 3){
            mViewModel.startAllServer();
        }else {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("请连接到WiFi网络后重试")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
            alertDialog.show();
        }

    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mViewModel.stopAllServer();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        mViewModel.stopAllServer();
    }
}